package com.bodu.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.bodu.core.common.AbstractCommonController;
import com.bodu.core.entity.CommonEntity;
import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.result.Result;
import com.bodu.global.annon.TraceLog;
import com.bodu.global.security.LoginUserUtil;
import com.bodu.system.entity.request.AdminRequest;
import com.bodu.system.entity.request.AdminUpdatePassRequest;
import com.bodu.system.entity.vo.AdminVo;
import com.bodu.system.entity.vo.DeptVo;
import com.bodu.system.enums.UpdatePassStatusEnums;
import com.bodu.system.service.AdminService;
import com.bodu.system.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 管理员管理 Biz
 *
 * @author gaoya
 * @since 2019-05-25
 */
@Api(tags = "管理员管理")
@RestController
@RequestMapping("/admin")
public class AdminController extends AbstractCommonController<AdminService, AdminRequest, AdminVo> {

    private final AdminService adminService;

    private final DeptService deptService;

    public AdminController(AdminService adminService, DeptService deptService) {
        this.adminService = adminService;
        this.deptService = deptService;
    }

    @TraceLog()
    @ApiOperation("修改密码")
    @PostMapping("updateP")
    public Result updatePass(@RequestBody AdminUpdatePassRequest adminUpdate) {
        Integer status = this.adminService.updatePassword(adminUpdate);
        if (status.equals(UpdatePassStatusEnums.OLD_PASS.getValue())) {
            return Result.fail(UpdatePassStatusEnums.OLD_PASS.getLabel());
        } else if (status.equals(UpdatePassStatusEnums.PASS_ERROR.getValue())) {
            return Result.fail(UpdatePassStatusEnums.PASS_ERROR.getLabel());
        } else {
            return Result.success(UpdatePassStatusEnums.PASS_SUCCESS.getLabel());
        }
    }
    @TraceLog("分页查询")
    @ApiOperation("管理员-分页")
    @PostMapping(value = "page")
    public Result page(@RequestBody ParamReq<AdminRequest> req)  {
        Long deptId = req.getEntity().getDeptId();
        if (ObjectUtil.isEmpty(deptId)) {
            List<DeptVo> depts = deptService.loopList();
            req.getEntity().setDeptIds(depts.stream().map(CommonEntity::getId).collect(Collectors.toSet()));
        }
        Set<Long> depts = req.getEntity().getDeptIds();
        if (ObjectUtil.isNotEmpty(deptId) && ObjectUtil.isEmpty(depts) ) {
            req.getEntity().setDeptIds(deptService.getDeptIdsByDept(deptId));
            req.getEntity().setDeptId(null);
        }
        return super.page(adminService, req);
    }

    @TraceLog()
    @ApiOperation("管理员-列表")
    @PostMapping(value = "listPage")
    public Result listPage(@RequestBody ParamReq<AdminRequest> req)  {
        return super.listPage(adminService, req);
    }

    @TraceLog()
    @ApiOperation("管理员-统计")
    @PostMapping(value = "countPage")
    public Result countPage(@RequestBody ParamReq<AdminRequest> req)  {
        return super.countPage(adminService, req);
    }

    @TraceLog()
    @ApiOperation("更新管理")
    @PutMapping
    public Result update(@RequestBody AdminRequest adminRequest) {
        return super.update(adminService, adminRequest);
    }

    @TraceLog()
    @ApiOperation("查询管理员详情")
    @GetMapping("{id}")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(adminService, id);
    }

    @TraceLog()
    @ApiOperation("保存管理员")
    @PostMapping
    public Result save(@RequestBody AdminRequest adminRequest) {
        return super.save(adminService, adminRequest);
    }

    @TraceLog()
    @ApiOperation("删除管理员")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return super.delete(adminService, id);
    }

    @TraceLog()
    @ApiOperation("批量删除管理员")
    @DeleteMapping("deletes/{ids}")
    public Result batchDelete(@PathVariable("ids") List<Long> ids) {
        return super.batchDelete(adminService, ids);
    }

    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }
}
